<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class AdminController extends BaseController {
    public function index(){
    	$this->assign('page_title','用户管理');
    	$AdminModel = D("Admin");
    	$items = $AdminModel->select();
    	$this->assign('volist',$items);
    	$this->assign('colNames',json_encode(array('管理员ID','管理员帐号','管理组别','管理员名称')));
    	//$this->assign('')
        $this->display();
    }
    
    public function getData(){
    	$GroupModel = D("Group");
    	$cate = $GroupModel->select();
    	$cate_arr = array();
    	foreach($cate as $v){
    		$cate_arr[$v['id']] = $v['name'];
    	}
    	$DataModel = D(CONTROLLER_NAME);
    	
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->page($page)->limit($limit)->select();
    	foreach($list as $vo){
    		$vo['group'] = $cate_arr[$vo['gpid']];
    		$items['rows'][] = $vo;
    	}
    	
    	$this->ajaxReturn($items);
    }
    
    public function add(){
    	if(!empty($_POST)) {
    		$AdminModel = D("Admin");
    		$data = $AdminModel->create($_POST);
    		$insert_id = $AdminModel->add();
    		if($insert_id){
    			//$this->success('组别新增成功！',__APP__.'/index.php/Group/index/');
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}else{
    		$this->assign('common_hide_header',true);
    		$GroupModel = D("Group");
    		$cate = $GroupModel->select();
    		$this->assign("list",$cate);
    		$this->display();
    	}
    }
    
    
    public function edit(){
    	$AdminModel = D("Admin");
    	if(!empty($_POST)) {
    		if(empty($_POST['login_pass']))unset($_POST['login_pass']);
    		$data = $AdminModel->create($_POST);
    		if(empty($data['login_pass']))unset($data['login_pass']);
    		$AdminModel->save($data);
    		
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>\$(\"#info-panel\").hide();\$(\"#layui-layer-shade3\").hide();\$table.trigger(\"reloadGrid\");</script>";exit;
    		
    	}else{
    		$item = $AdminModel->where("id=".(int)$_GET['id'])->select();
    		$this->assign('vo',$item[0]);
    		//目录
    		$GroupModel = D("Group");
    		$cate = $GroupModel->select();
    		$this->assign('list',$cate);
    		$this->display();
    	}
    }
    
    public function email(){
    	$SliderModel = D("Sliders");
    	if(!empty($_POST)) {
    		if(!empty($_POST['id'])){
    			$result = $SliderModel->where("id=".(int)$_POST['id'])->save(array('title'=>$_POST['title']));
    		}else{
    			$result = $SliderModel->add(array('ntype'=>4,'title'=>$_POST['title']));
    		}

    		header("Content-type: text/html; charset=utf-8");
    		echo "<script language=\"\">alert('更新成功');window.location.href='".__APP__."/index.php/Admin/email';</script>";exit;
    	}else{
    		$item = $SliderModel->where("ntype=4")->find();
    		$this->assign('vo',$item);
    		$this->display();
    	}
    }
}